<template>
<div class="main_box" style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="roleForm" ref="roleForm">
      <el-row>
        <el-transfer v-model="roleForm.rids" :data="roleForm.roles" :titles="['全部角色', '该用户拥有角色']"></el-transfer>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import AuthApi from "@/api/system/auth";
export default{
  data(){
    return {
      roleForm:{
        rids: [],
        roles: [],
      },
    }
  },
  components: {
  },
  mounted(){
    this.getRoleData();
  },
  activated(){
    if(this.$route.meta.keepAlive){
      this.listTable();
    }
  },
  methods: {
    getRoleData(){
      AuthApi.getUserRoleList({userId:this.$route.query.id})
      .then(res => {
        res.data.roles.forEach(item => {
          this.roleForm.roles.push({
            label: item.roleName,
            key: item.roleId,
          })
        })
        this.roleForm.rids = res.data.userRoles;
      })
    },
    sureClick(){
      let data = {
        userId: this.$route.query.id,
        rids: this.roleForm.rids,
        roles: this.roleForm.roles,
      };
      AuthApi.addUserRoleList(data)
      .then(res => {
        this.$Utils.notifySuccess()
        this.returnList()
      })
    },
    returnList(){
        this.$store.dispatch('delView', this.$route);
        this.$router.push({path: "/sys/userManagement"});
    }
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
